package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.config.RedisConfig;
import cn.wolfcode.luowowo.cache.handler.StrategyStatisHandler;
import cn.wolfcode.luowowo.cache.service.IRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Administrator on 2019/7/13.
 */
@Component
@Import(RedisConfig.class)
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IRedisService redisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            if (redisService.exist(StrategyStatisHandler.STRATEGY_STATIS_VO,detail.getId()+"")){
                continue;
            }
            vo = new StrategyStatisVO();
            vo.setIsabroad(detail.isIsabroad());
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setTitle(detail.getTitle());
            vo.setStrategyId(detail.getId());
            vo.setThumbsupnum(detail.getThumbsupnum());
            vo.setSharenum(detail.getSharenum());
            vo.setReplynum(detail.getReplynum());
            vo.setFavornum(detail.getFavornum());
            vo.setViewnum(detail.getViewnum());
            strategyStatisRedisService.setStatisVo(vo);



            //将收藏数和点赞数,初始化到redis中
            //strategyStatisRedisService.addScore(StrategyStatisHandler.STRATEGY_STATIS_COMMEND_SORT,detail.getId(),0);
            //将评论数和浏览数,初始化到redis中
            //strategyStatisRedisService.addScore(StrategyStatisHandler.STRATEGY_STATIS_COMMEND_HOT,detail.getId(),0);
        }
        //完整版初始化数据
        //进行初始化之前,我们先去redis中查看key对应的value是否有值,如果有值就跳过初始化过程;如果没有就进行初始化
        redisInitStrategy(list,StrategyStatisHandler.STRATEGY_STATIS_COMMEND_SORT,false);
        redisInitStrategy(list,StrategyStatisHandler.STRATEGY_STATIS_COMMEND_HOT,true);
    }
    private void redisInitStrategy(List<StrategyDetail> list, StrategyStatisHandler handler,boolean flag) {
        for (StrategyDetail detail : list) {
            //zrank key value
            String key = handler.getKeyPrefix()+"";
            String value = StrategyStatisHandler.STRATEGY_STATIS_VO.getKeyPrefix()+detail.getId();
            if (!redisService.hasZsetValue(key,value)){
                //如果返回值为true,代表已经存在,那么我们不做任何操作,如果不存在,那么我们就初始化
                int score = flag?detail.getFavornum()+detail.getThumbsupnum():detail.getViewnum()+detail.getReplynum();
                strategyStatisRedisService.addScore(handler,detail.getId(),score);
            }
        }
    }
}
